<!--
 * @Descripttion: 模具履历
 * @LastEditors: xzh
 * @LastEditTime: 2023-11-12 23:07:33
-->
<template>
  <div class="resume-container" v-loading="moldResumeParam.loading">
    <!-- 简介 -->
    <ResumeIntro :formData="moldResumeParam.formData" />
    <!-- 模具信息 -->
    <ResumeInfo
      :formData="moldResumeParam.formData"
      :resoure="moldResumeParam.resoure"
      @sure="hanldeSure"
    />
    <!-- 维修记录 -->
    <ResumeRepairRecord ref="resumeRepairRecordRef" />
    <!-- 保养计划 -->
    <ResumeUpKeepRecord ref="resumeUpKeepRecordRef" />
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, toRef, toRefs, nextTick, onMounted } from "vue";
import type { object_Inf } from "@/utils/typings/index.type";
import { useI18n } from "vue-i18n";
import { ElNotification } from "element-plus";
import ResumeIntro from "./resume-intro/index.vue";
import ResumeInfo from "./resume-info/index.vue";
import ResumeRepairRecord from "./resume-repairRecord/index.vue";
import ResumeUpKeepRecord from "./resume-upKeepRecord/index.vue";
import Dile_APi from "@/api";
import Dile_Methods from "@/utils/methods";
let { t } = useI18n();
/**
 * @Author: xzh
 * @Descripttion: 维修记录实例
 * @Param:
 */
let resumeRepairRecordRef = ref<any>(null);
/**
 * @Author: xzh
 * @Descripttion: 保养记录实例
 * @Param:
 */
let resumeUpKeepRecordRef = ref<any>(null);
/**
 * @Author: xzh
 * @Descripttion: 表单默认数据
 * @Param:
 */
let defaultData = () => ({
  id: 0,
  code: "", //模具编码
  name: "", //模具名称
  specification: "", //规格型号
  typeId: null, //模具类型
  positionId: null, //存放位置
  remark: "", //备注
  createTime: "",
  createUser: "",
  updateTime: "",
  updateUser: "",
  extJson: {
    typeName: "", //模具类型名称
    positionName: "", //存放位置名称
  },
});
/**
 * @Author: xzh
 * @Descripttion: 模具履历参数
 * @Param:
 */
let moldResumeParam = reactive<object_Inf>({
  loading: false,
  //表单数据
  formData: defaultData(),
  resoure: {
    //存放位置
    positionArr: [],
  },
});

/**
 * @Author: xzh
 * @Descripttion:初始化加载
 * @Param:
 * @param {*} id 模具档案主键
 */
let init = async (id: number) => {
  let { loading } = toRefs(moldResumeParam);
  try {
    loading.value = true;
    //加载表单资源
    await initSearchResource();
    //加载表单数据
    await initFormData(id);
    //加载维修记录
    resumeRepairRecordRef.value.init(id);
    //加载保养记录
    resumeUpKeepRecordRef.value.init(id);
  } finally {
    loading.value = false;
  }
};

/**
 * @Author: xzh
 * @Descripttion: 加载表单数据
 * @Param:
 */
let initFormData = async (id: number) => {
  let data: object_Inf = !!id
    ? await Dile_APi.getMoldArchivesInfo(id)
    : defaultData();
  Object.assign(moldResumeParam.formData, data);
};

/**
 * @Author: xzh
 * @Descripttion: 加载表单资源
 * @Param:
 */
let initSearchResource = async () => {
  //加载存放位置
  await initPositionResource();
};

/**
 * @Author: xzh
 * @Descripttion: 加载存放位置
 * @Param:
 */
let initPositionResource = async () => {
  let positionArr = toRef(moldResumeParam.resoure, "positionArr");
  //加载所有存放位置
  positionArr.value = Dile_Methods.filterDataArr(
    (await Dile_APi.getPositionList()).map(({ id, position }: object_Inf) => ({
      value: id,
      label: position,
    }))
  );
};

/**
 * @Author: xzh
 * @Descripttion: 保存事件
 * @Param:
 */
let hanldeSure = async () => {
  let { formData, loading } = toRefs(moldResumeParam);
  if (!!loading.value) {
    return false;
  }
  try {
    loading.value = true;
    await Dile_APi.saveMoldArchives(formData.value);
    //提示成功
    ElNotification({
      message: t("common.message.OperationSuccess"),
      type: "success",
    });
  } finally {
    loading.value = false;
  }
};

defineExpose({
  init,
});
</script>

<style lang="scss" scoped>
.resume-container {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}
</style>
